package com.guch.help.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.guch.help.R;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class TestLoginActivity extends AppCompatActivity {

    private static final String APP_ID = "wx6b041996282e4013";
    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_login);

        Button btnLogin = findViewById(R.id.btn_wx_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginWithWeChat();
            }
        });

        registerWx();


    }

    /**
     * 注册微信APPID
     */
    private void registerWx() {
        // 替换成你在微信开放平台注册的AppID
        api = WXAPIFactory.createWXAPI(this, APP_ID, true);
        api.registerApp(APP_ID);
    }

    private void loginWithWeChat() {
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo"; // 请求获取用户信息
        req.state = "wechat_login"; // 自定义状态值，可以用于区分不同的登录场景
        if(api != null) {
            api.sendReq(req);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        this.setIntent(intent);
//        if (api.handleIntent(intent, this)) {
//            // 处理返回数据
//
//        }
    }
}
